package com.example.springbootbookv2.exception;

import org.springframework.http.HttpStatus;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;


/**
 * 用于捕捉Exception异常以及其子类
 * 捕捉到之后，把异常信息，发出异常的地址放进ModelAndView里，然后跳转到 errorPage.html
 * @author waqwb
 */
@ControllerAdvice
public class GlobalExceptionHandler {
    @ExceptionHandler(Throwable.class)
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
    public String dealException(final Throwable throwable, final Model model){
        String errorMessage = (throwable != null ? throwable.getMessage() : "Unknown error");
        model.addAttribute("errorMessage", errorMessage);
        return "errorPage";
    }
}
